﻿
using Service.Framework;
using Service.Framework.ApplicationEventBus;
using Service.Framework.SmsRecordLog;
using TencentCloud.Common;
using TencentCloud.Sms.V20210111;
using TencentCloud.Sms.V20210111.Models;

namespace Service.Core.Utils.Sms
{
    /// <summary>
    /// 腾讯云短信服务
    /// </summary>
    public class TencentCloudSmsService : ISmsService
    {
        private readonly SmsClient smsClient;
        private readonly IApplicationEventBus applicationEventBus;
        private readonly AppSettings<TencentCloudSmsConfig> appSettings;

        public TencentCloudSmsService(IApplicationEventBus applicationEventBus, AppSettings<TencentCloudSmsConfig> appSettings)
        {
            this.applicationEventBus = applicationEventBus;
            this.appSettings = appSettings;
            smsClient = new SmsClient(new Credential
            {
                SecretId = appSettings.Instance.SecretId,
                SecretKey = appSettings.Instance.SecretKey
            }, "ap-guangzhou");

        }

        public async Task<bool> SendSms(string phone, string templateTypeId, params string[] templateParam)
        {
            List<SmsRecordLogAges> records = new List<SmsRecordLogAges>();
            try
            {
                var smsTemplate = appSettings.Instance.SmsTemplates.FirstOrDefault(s => s.TemplateTypeId.Equals(templateTypeId));
                if (smsTemplate == null)
                {
                    throw new LogicErrorException("发送短信失败，模板配置不存在。");
                }
                SendSmsRequest sendSmsRequest = new SendSmsRequest()
                {
                    SmsSdkAppId = smsTemplate.AppId,
                    SignName = smsTemplate.SignName,
                    TemplateId = smsTemplate.TemplateId,
                    TemplateParamSet = templateParam,
                    PhoneNumberSet = [phone],
                };
                var sendSmsResponse = await smsClient.SendSms(sendSmsRequest);
                foreach (var item in sendSmsResponse.SendStatusSet)
                {
                    records.Add(ConventToSmsRecordLogAges(phone, smsTemplate.SignName, smsTemplate.TemplateName, item, templateParam));
                }
                return true;
            }
            catch (Exception e)
            {
                records.Add(ConventToSmsRecordLogAges(phone, "", "", new SendStatus()
                {
                    Code = "Error",
                    Message = e.Message,
                    PhoneNumber = phone,
                }, templateParam));
                return false;
            }
            finally
            {
                foreach (var item in records)
                {
                    applicationEventBus.PublishAsync(item);
                }

            }
        }


        private SmsRecordLogAges ConventToSmsRecordLogAges(string phone, string signName, string templateName, SendStatus sendStatus, params string[] templateParam)
        {
            var smsRecordLogDto = new SmsRecordLogDto("腾讯云", templateName, string.Join("、", templateParam), signName, phone, "Ok".Equals(sendStatus.Code), sendStatus.Message);
            var smsRecordLogAges = new SmsRecordLogAges(smsRecordLogDto);
            return smsRecordLogAges;

        }
    }
}
